package com.mingyizhudao.app.view.home;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.http.DataHandler;
import com.dixiang.framework.utils.Utils;
import com.dixiang.framework.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseFragment;
import com.mingyizhudao.app.view.WebViewActivity;
import com.mingyizhudao.app.view.booking.FastBookingActivity;
import com.mingyizhudao.app.view.doctor.DoctorListActivity;
import com.mingyizhudao.app.view.home.bean.BannerEntity;
import com.mingyizhudao.app.view.home.bean.DisNavsEntity;
import com.mingyizhudao.app.view.home.bean.ScalpelEntity;
import com.mingyizhudao.app.view.search.SearchActivity;

public class HomeFragment extends MYBaseFragment {

	private Context context;
	private ScalpelEntity scalpelEntity;
	private Map<String,DisNavsEntity> mapDisNav = new HashMap<String , DisNavsEntity>();
	private LinearLayout ll_bottom ;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.fragment_home, container, false);
		initView(mView);
		loadInitData();
		return mView;
	}

	private void loadInitData() {
		try {
			if (scalpelEntity == null || Constants.IS_SCALPEL_NEAD_REFRESH) {
				try {
					scalpelEntity = (ScalpelEntity) Utils
							.getDataFromCache(HomeFragment.class
									.getSimpleName());
				} catch (Exception ex) {
					ex.printStackTrace();
				}


				if (scalpelEntity != null) {
					 ll_bottom.setVisibility(View.VISIBLE);
					List<BannerEntity> banners = scalpelEntity.getBanners();
					putMap(scalpelEntity.getDisNavs());

				}
				doNet();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden) {
			if (scalpelEntity==null) {
				doNet();
			}
		}
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void initHeaderView() {
		setTopViewIsShow(true);
		clearTitle();
		hidHeaderView();
//		setTopViewBg(R.color.actionbar_bg_color);
//		View view = activity.getLayoutInflater().inflate(R.layout.home_title_search, null,false);
//		initCenterView(view);
//		TextView tv_search =  (TextView) view.findViewById(R.id.tv_search);
//		tv_search.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (scalpelEntity != null) {
//					Bundle bundle = new Bundle();
//					bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) scalpelEntity.getDisNavs());
//					bundle.putString(Constants.PARM_ACTION_URL, scalpelEntity.url.getDoctor());
//					
//					IntentHelper.getInstance(getActivity()).gotoActivity(SearchActivity.class, bundle);
//
//				} else {
//					Toast.makeText(context, "数据未加载完毕！", 0).show();
//				}
//			}
//		});
	}

	/*
	 * 初始化界面
	 */
	private void initView(View mView) {

		context = getActivity();
	    ll_bottom = (LinearLayout) mView.findViewById(R.id.ll_bottom);
	    ll_bottom.setVisibility(View.GONE);
	    
	    mView.findViewById(R.id.search).setOnClickListener(onClickListener);	
	    
		mView.findViewById(R.id.go_doctor).setOnClickListener(onClickListener);
		mView.findViewById(R.id.go_benefit).setOnClickListener(onClickListener);
		mView.findViewById(R.id.go_fast_booking).setOnClickListener(onClickListener);
		
		mView.findViewById(R.id.dpt_waike).setOnClickListener(onClickListener);
		mView.findViewById(R.id.dpt_guke).setOnClickListener(onClickListener);
		mView.findViewById(R.id.dpt_fuchanke).setOnClickListener(onClickListener);
		mView.findViewById(R.id.dpt_xiaoerke).setOnClickListener(onClickListener);
		mView.findViewById(R.id.dpt_wuguanke).setOnClickListener(onClickListener);
		mView.findViewById(R.id.dpt_neike).setOnClickListener(onClickListener);	
		
	}

	private void toDeptToHospitalActivity(int id) {
		if (scalpelEntity != null) {
			DisNavsEntity disNavsEntity = null;
			switch (id) {
				case R.id.dpt_waike:
					disNavsEntity = mapDisNav.get("外科");
					break;
				case R.id.dpt_guke:
					disNavsEntity = mapDisNav.get("骨科");
					break;
				case R.id.dpt_fuchanke:
					disNavsEntity = mapDisNav.get("妇产科");
					
					break;
				case R.id.dpt_xiaoerke:
					disNavsEntity = mapDisNav.get("小儿外科");
					
					break;
				case R.id.dpt_wuguanke:
					disNavsEntity = mapDisNav.get("五官科");
					
					break;
				case R.id.dpt_neike:
					disNavsEntity = mapDisNav.get("内科");
					
					break;
				default:
					break;
			}
			Bundle bundle = new Bundle();
			bundle.putSerializable("disNavsEntity",(Serializable) disNavsEntity);
			IntentHelper.getInstance(getActivity()).gotoActivity(DeptToHospitalActivity.class, bundle);
		} else {
			Toast.makeText(context, "数据未加载完毕！", 0).show();
		}
	}
	
	private void doNet() {
		LoadingDialog.getInstance(activity).show();
		String actionUrl = Constants.baseURL + "appnav1";// + "&api=6"

		QJNetUICallback callback = new QJNetUICallback<QjResult<ScalpelEntity>>(
				activity) {
			@Override
			public void onSuccess(QjResult<ScalpelEntity> result) {
				try {
					if (result != null) {
						scalpelEntity = result.getResults();
						if (scalpelEntity != null) {
							ll_bottom.setVisibility(View.VISIBLE);
							putMap(scalpelEntity.getDisNavs());
							Utils.saveDataToCache(
									HomeFragment.class.getSimpleName(),
									scalpelEntity);
						}else {
							Toast.makeText(context, "数据加载失败！", 0).show();
						}
					}

				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

			public void onError(Exception e, QjResult<ScalpelEntity> result) {
				super.onError(e, result);
			};

			public void onCompleted(Exception e, QjResult<ScalpelEntity> result) {
				super.onCompleted(e, result);
			};
		};
		String path = new DataHandler().getDataObject(activity, actionUrl,
				null, new TypeToken<QjResult<ScalpelEntity>>() {
				}, callback);
	}
	
	private void putMap(List<DisNavsEntity> disNavs){
		if(disNavs == null || disNavs.size() == 0) return;
		for (DisNavsEntity disNavsEntity : disNavs) {
			String key = disNavsEntity.getName();
			if(!mapDisNav.containsKey(key)){
				mapDisNav.put(key, disNavsEntity);
			}
		}
		
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.go_doctor:
				if (scalpelEntity != null&&scalpelEntity.url!=null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) scalpelEntity.getDisNavs());
					bundle.putString(Constants.PARM_ACTION_URL, scalpelEntity.url.getDoctor());
				//	bundle.putString(Constants.PARM_ACTION_URL, "http://api.mingyizhudao.com/api2/doctor");
					
					IntentHelper.getInstance(getActivity()).gotoActivity(DoctorListActivity.class, bundle);

				} else {
					Toast.makeText(context, "数据未加载完毕！", 0).show();
				}
				break;
			case R.id.go_benefit:
				if (scalpelEntity != null&&scalpelEntity.url!=null) {
				Bundle bundle = new Bundle();
				bundle.putString(Constants.PARM_ACTION_URL, scalpelEntity.url.getBenefit());
				bundle.putString(Constants.PARM_PAGE_TITLE, "名医公益");
				IntentHelper.getInstance(activity).gotoActivity(WebViewActivity.class, bundle);
				}
				else {
					Toast.makeText(context, "数据未加载完毕！", 0).show();
				}
				break;
			case R.id.go_fast_booking:
				if (scalpelEntity != null&&scalpelEntity.url!=null) {
				IntentHelper.getInstance(activity).gotoActivityWithURL(
						FastBookingActivity.class, scalpelEntity.url.getQuickbooking());
				}
				else {
					Toast.makeText(context, "数据未加载完毕！", 0).show();
				}
				break;
			
			case R.id.search:
				if (scalpelEntity != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable(Constants.ACTION_URL_DISNAVS,(Serializable) scalpelEntity.getDisNavs());
					bundle.putString(Constants.PARM_ACTION_URL, scalpelEntity.url.getDoctor());
					
					IntentHelper.getInstance(getActivity()).gotoActivity(SearchActivity.class, bundle);

				} else {
					Toast.makeText(context, "数据未加载完毕！", 0).show();
				}
				break;	
			default:
				toDeptToHospitalActivity(arg0.getId());
				break;
			}
		}
	};
	
	@Override
	public void onPause() {
		super.onPause();
	}

}
